<template>
    <div class="container">
        <div class="panelBody">
                <div class="navMenv">
                    <h1 class="loginTitle">账号登录</h1>
                </div> 
                <div class="loginPhoneNumber">   
                    <el-input
                        placeholder="电话号码"
                        v-model="phoneNumber"
                        clearable>
                    </el-input>
                </div>
                <div class="loginSecurityCode">   
                    <el-input
                        placeholder="请输入密码"
                        v-model="passWords"
                        type="password"
                        clearable>
                    </el-input>
                </div>
                <div>
                    <el-row>
                        <el-button type="primary" @click="post" class="loginButton">登录</el-button>
                    </el-row>
                </div>
                <div class="registreBlock">
                    <p><router-link to="/register" class="registrePath">无帐号去注册</router-link></p>
                </div>
        </div>
    </div>
</template>
<script>
export default {
    data (){
        return {
            phoneNumber:'',
            passWords:'',
        }
    },
    mounted:function(){
        let that = this;
        this.handLogin(that);
    },
    methods: {
        //登录
        post(){
            var self = this;
            var reg = 11&& /^((13|14|15|17|18|19|16|12|11|10)[0-9]{1}\d{8})$/;//手机号正则验证
            var regPassWord = /^\d{6,12}$/
            var phoneNum = this.phoneNumber;
            var psw = this.passWords;
            if(!phoneNum){//未输入手机号
                this.$message.error("请输入手机号码");
                return;
            }
            if(!reg.test(phoneNum)){//手机号不合法
                this.$message.error("您输入的手机号码不合法，请重新输入");
                return;
            }
            if(!psw){//未输入密码
                this.$message.error("请输入密码");
                return;
            }
            if(!regPassWord.test(psw)){//密码格式不合法}
                this.$message.error("请输入6-12数字");
                return;
            }
            this.$http({
                method:'post',
                url:'/passport/login',
                data:{
                        "phone":this.phoneNumber,
                        "password":this.passWords,
                        },
            }).then(function(res){
                let data = res.data;
                if(data.status === 200){
                    self.$message.success(data.message);
                    self.$router.push({ path: "/choicefirm" });

                }else{
                    self.$message.error(data.message);
                }
            }).catch(function(err){
                console.log(err)
            })
        },
        //回车登录
        handLogin(that){
            document.body.addEventListener('keyup', function (e) {
            if (e.keyCode == '13') {
                console.log(13);
                var self = that;
                var reg = 11&& /^((13|14|15|17|18|19|16|12|11|10)[0-9]{1}\d{8})$/;//手机号正则验证
                var regPassWord = /^\d{6,12}$/
                var phoneNum = that.phoneNumber;
                var psw = that.passWords;
                if(!phoneNum){//未输入手机号
                    that.$message.error("请输入手机号码");
                    return;
                }
                if(!reg.test(phoneNum)){//手机号不合法
                    that.$message.error("您输入的手机号码不合法，请重新输入");
                    return;
                }
                if(!psw){//未输入密码
                    that.$message.error("请输入密码");
                    return;
                }
                if(!regPassWord.test(psw)){//密码格式不合法}
                    that.$message.error("请输入6-12数字");
                    return;
                }
                that.$http({
                    method:'post',
                    url:'/passport/login',
                    data:{
                            "phone":that.phoneNumber,
                            "password":that.passWords,
                            },
                }).then(function(res){
                    let data = res.data;
                    if(data.status === 200){
                        self.$message.success(data.message);
                        self.$router.push({ path: "/choicefirm" });

                    }else{
                        self.$message.error(data.message);
                    }
                }).catch(function(err){
                    console.log(err)
                })
                }
        });
        },
    }
}
</script>

